package com.inossem.warehousestorage_inossem.adapter.emergencyWarehousing;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.emergency_warehousing.EmergencyWarehousingReceiveAddOperatorActivity;
import com.inossem.warehousestorage_inossem.activity.emergency_warehousing.EmergencyWarehousingReceiveSelectOperatorActivity;
import com.inossem.warehousestorage_inossem.activity.emergency_warehousing.extra.EmergencyWarehousingExtra;
import com.inossem.warehousestorage_inossem.activity.in_storage.extra.InStorageExtra;
import com.inossem.warehousestorage_inossem.bean.common.GetOperatorBean;
import com.inossem.warehousestorage_inossem.bean.common.GetRoleBean;
import com.inossem.warehousestorage_inossem.bean.emergencyWarehousing.EmergencyWarehousingReceiveOverviewBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.utils.EmergencyWarehousingUtils;

import java.util.List;

/**
 * 作者：wsy on 2017/11/28 19:32
 * <p>
 * 邮箱：445979770@qq.com
 */

public class EmergencyReceiveOperatorListAdapter extends BaseAdapter {

    private Context mContext;
    private List<GetOperatorBean.OperatorBean> mOperatorList;
    private List<GetRoleBean.RoleBean> mRoleList;
    private LayoutInflater inflater;
    private String mReceipNumber, mEmergencyType, mCreatTime , statusCode , mRoleName, mRoleCode;
    private EmergencyWarehousingReceiveAddOperatorActivity.EmergencyWarehousingReceiveAddOperatorListener mInStorageReceiveAddOperatorListener;

    public EmergencyReceiveOperatorListAdapter(Context context, EmergencyWarehousingReceiveOverviewBean data,List<GetRoleBean.RoleBean> roleList , String roleName, String roleCode , String receipNumber,  EmergencyWarehousingReceiveAddOperatorActivity.EmergencyWarehousingReceiveAddOperatorListener inStorageReceiveAddOperatorListener) {
        mContext = context;
        mReceipNumber = receipNumber;
        mEmergencyType = data.getBody().getReceipt_type_name();
        mCreatTime = data.getBody().getCreate_time();
        mOperatorList = data.getBody().getUser_list();
        statusCode = data.getBody().getStatus_code();
        mRoleList = roleList;
        mInStorageReceiveAddOperatorListener = inStorageReceiveAddOperatorListener;
        inflater = LayoutInflater.from(context);
        this.mRoleCode = roleCode;
        this.mRoleName = roleName;
    }

    @Override
    public int getCount() {
        return mOperatorList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return mOperatorList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.view_emergency_warehousing_receive_add_operator_list_first_item, null);
            TextView receipNumber = (TextView) convertView.findViewById(R.id.receipNumber);
            TextView emergencyType = (TextView) convertView.findViewById(R.id.emergencyType);
            TextView creatTime = (TextView) convertView.findViewById(R.id.creatTime);
            TextView roleName = (TextView) convertView.findViewById(R.id.roleName);
            TextView roleCode = (TextView) convertView.findViewById(R.id.roleCode);
            EditText condition = (EditText) convertView.findViewById(R.id.condition);
            ImageView search = (ImageView) convertView.findViewById(R.id.search);
            receipNumber.setText(mReceipNumber);
            emergencyType.setText(mEmergencyType);
            creatTime.setText(mCreatTime);
            roleName.setText(TextUtils.isEmpty(mRoleName) ? mContext.getResources().getString(R.string.activity_receive_add_receiver_choose_role) : mRoleName);
            roleCode.setText(TextUtils.isEmpty(mRoleCode) ? "" : mRoleCode);
            roleName.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    DialogUtils.getRoleaDialogNew(mContext, mRoleList, new DialogUtils.RoleListener() {
                        @Override
                        public void onSelect(Dialog dialog, String roleNameStr, String roleCodeStr) {
                            roleName.setText(roleNameStr);
                            roleCode.setText(roleCodeStr);
                            mInStorageReceiveAddOperatorListener.onSelectRole(roleNameStr, roleCodeStr);
                        }
                    }).show();
                }
            });
//            roleName.setText(mRoleList.get(0).getRole_name());
//            roleCode.setText(mRoleList.get(0).getRole_id());
//            if (statusCode.equals("1")) {
//                roleName.setOnClickListener(new NoDoubleClickListener() {
//                    @Override
//                    public void onNoDoubleClick(View v) {
//                        mInStorageReceiveAddOperatorListener.onSelectRole("领料人", "35");
//                    }
//                });
//            }else {
//                Toast.makeText(mContext , "当前状态不能修改经办人" , Toast.LENGTH_SHORT).show();
//            }

            search.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    Utils.closeSoftKeyboard(mContext, v);
                    if (statusCode.equals("1")) {
                        String roleCodeStr = roleCode.getText().toString();
//                    if (TextUtils.isEmpty(roleCodeStr)) {
//                        Utils.middleToast(mContext, mContext.getResources().getString(R.string.activity_receive_add_receiver_choose_role));
//                        return;
//                    }
                        String conditionStr = condition.getText().toString();
                        if (TextUtils.isEmpty(conditionStr)) {
                            Utils.middleToast(mContext, mContext.getResources().getString(R.string.activity_receive_add_receiver_condition_null));
                            return;
                        }
                        Intent intent = new Intent(mContext, EmergencyWarehousingReceiveSelectOperatorActivity.class);
                        intent.putExtra(InStorageExtra.IN_STORAGE_CONDITION, conditionStr);
                        intent.putExtra(InStorageExtra.IN_STORAGE_ROLE, roleCodeStr);
                        intent.putExtra(EmergencyWarehousingExtra.STATUS_CODE, statusCode);
                        mContext.startActivity(intent);
                    }else {
                        Toast.makeText(mContext , mContext.getResources().getString(R.string.activity_emergency_warehousing_receive_add_operator_cannot_be_modified) , Toast.LENGTH_SHORT).show();
                    }

                }
            });
        } else {
            GetOperatorBean.OperatorBean operatorItem = mOperatorList.get(position - 1);
            EmergencyReceiveOperatorListAdapter.ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.view_in_storage_receive_add_operator_list, null);
                viewHolder = new EmergencyReceiveOperatorListAdapter.ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (EmergencyReceiveOperatorListAdapter.ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, operatorItem);
        }
        return convertView;
    }

    private void initView(EmergencyReceiveOperatorListAdapter.ViewHolder viewHolder, View view) {
        viewHolder.id = (TextView) view.findViewById(R.id.id);
        viewHolder.name = (TextView) view.findViewById(R.id.name);
        viewHolder.roleName = (TextView) view.findViewById(R.id.roleName);
        viewHolder.department = (TextView) view.findViewById(R.id.department);
    }

    private void initData(Context context, EmergencyReceiveOperatorListAdapter.ViewHolder viewHolder, View view, GetOperatorBean.OperatorBean operatorItem) {
        viewHolder.id.setText(operatorItem.getUser_id());
        viewHolder.name.setText(operatorItem.getUser_name());
        viewHolder.roleName.setText(operatorItem.getRole_name());
        viewHolder.department.setText(operatorItem.getOrg_name());

            view.setOnLongClickListener((View v) -> {
                if (statusCode.equals("1")) {
                    DialogUtils.getConfirmDialog(mContext, mContext.getResources().getString(R.string.activity_emergency_warehousing_receive_add_operator_confirm_to_delete) + operatorItem.getUser_name(), new DialogUtils.ConfirmDialogListener() {
                        @Override
                        public void onConfirm(Dialog dialog) {
                            EmergencyWarehousingUtils.deleteReceiveOperator(mContext, operatorItem.getUser_id());
                            dialog.cancel();
                            mInStorageReceiveAddOperatorListener.onDeleteOperatorListener();
                        }
                    }).show();
                }else {
                    Toast.makeText(context , mContext.getResources().getString(R.string.activity_emergency_warehousing_receive_add_operator_cannot_be_modified) , Toast.LENGTH_SHORT).show();
                }
                return true;

            });


    }
    private class ViewHolder {
        private TextView id, name, roleName, department;
    }

}
